/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> two_lex.c </file>
  <brief>
    Read from 2 instances of uls simultaenously.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "uls.h"

#include "sam1_lex.h"
#include "sam2_lex.h"
#include "uls.h"

#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>

char *progname;
int  opt_verbose;

uls_lex_t *sam1_lex, *sam2_lex;

static void usage(void)
{
	err_log("%s v1.0", progname);
	err_log("  ULS can instantiate many uls lexical objects.");
	err_log("  '%s' demonstrates this feature of ULS", progname);
	err_log("");
	err_log(" Usage:");
	err_log("  %s <inputfile-1> ...", progname);
	err_log("");
	err_log("  For example,");
	err_log("      %s input1.txt", progname);
	exit(1);
}

static int
parse_options(int argc, char* argv[])
{
	int   opt;

	while ((opt=getopt(argc, argv, "c:vh")) != -1) {
		switch (opt) {
		case 'c':
			break;

		case 'v':
			opt_verbose = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			err_log("undefined option -%c", opt);
			return -1;
			break;
		}
	}

	return optind;
}
void
test_uls_2(char* fpath)
{
	int i;

	if (uls_push_file(sam2_lex, fpath, 0) < 0) {
		err_log(" file open error");
		return;
	}

	for (i=0; ; ) {
		if (uls_gettok(sam2_lex) == T_EOI) break;
		// Make sure not to use TOK_EOI rather than T_EOI.

		uls_printf("\t\t[#%3d] %3d", uls_get_lineno(sam2_lex), sam2_lex->tok);
		uls_prntok(sam2_lex);

		++i;
	}
}

void
test_uls(char* fpath)
{
	int fd;
	int i;

	if ((fd=open(fpath, O_RDONLY)) < 0) {
		err_log(" file open error");
		return;
	}

	uls_push_fd(sam1_lex, fd, 0);

	for (i=0; ; ) {
		if (uls_gettok(sam1_lex) == TOK_EOI) break;

		uls_printf("\t[#%3d] %3d", uls_get_lineno(sam1_lex), sam1_lex->tok);
		uls_prntok(sam1_lex);

		if (sam1_lex->tok == TOK_ID && uls_streql(uls_lexeme_text(sam1_lex), "WWW")) {
			test_uls_2("input2.txt");
		}

		++i;
	}
	
	close(fd);
}

int
main(int argc, char* argv[])
{
	char *conf_name1 = "sam1.ulc";
	char *conf_name2 = "sam2.ulc";
	char *input_file;
	int i0;

	progname = uls_filename(argv[0]);

	if ((i0=parse_options(argc, argv)) <= 0 || i0 >= argc) {
		usage();
		return -1;
	}
	input_file = argv[i0];

	if ((sam1_lex=uls_create(conf_name1)) == NULL) {
		err_log("can't init uls-lexer");
		return 1;
	}

	if ((sam2_lex=uls_create(conf_name2)) == NULL) {
		err_log("can't init uls-lexer");
		uls_destroy(sam1_lex);
		return 1;
	}

	test_uls(input_file);

	uls_destroy(sam1_lex);
	uls_destroy(sam2_lex);

	return 0;
}
